(function (vc) {
    vc.extends({
        propTypes: {
            callBackListener: vc.propTypes.string, //父组件名称
            callBackFunction: vc.propTypes.string //父组件监听方法
        },
        data: {
            addTelHousekeepingInfo: {
                poolId: '',
                houseTypeId: '',
                servId: '',
                servName: '',
                shopId: vc.getCurrentCommunity().shopId,
                applyName: '',
                applyTel: '',
                appointmentTime: '',
                context: '',
                housekeepingServs: [],
                repairSettings: []
            }
        },
        _initMethod: function () {
            $that._initAddOwnerRepairInfo();
        },
        _initEvent: function () {
            vc.on('addOwnerRepair', 'openAddOwnerRepairModal', function (_ownerInfo) {
                $('#addOwnerRepairModel').modal('show');
            });
            //加载报修类型
            $that._listRepairSettings(1, 50);
        },
        methods: {
            addOwnerRepairValidate() {
                return vc.validate.validate({
                    addTelHousekeepingInfo: $that.addTelHousekeepingInfo
                }, {
                    'addTelHousekeepingInfo.servId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "报修类型不能为空"
                        },
                        {
                            limit: "maxin",
                            param: "2,50",
                            errInfo: "报修类型错误"
                        },
                    ],
                    'addTelHousekeepingInfo.applyName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "报修人不能为空"
                        },
                        {
                            limit: "maxin",
                            param: "2,50",
                            errInfo: "报修人名称必须在2至50字符之间"
                        },
                    ],
                    'addTelHousekeepingInfo.applyTel': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "联系方式不能为空"
                        },
                        {
                            limit: "phone",
                            param: "",
                            errInfo: "联系方式格式不正确"
                        },
                    ],
                    'addTelHousekeepingInfo.appointmentTime': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "预约时间不能为空"
                        },
                        {
                            limit: "dateTime",
                            param: "",
                            errInfo: "预约时间格式错误"
                        },
                    ],
                    'addTelHousekeepingInfo.context': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "报修内容不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "2000",
                            errInfo: "报修内容不能超过2000"
                        },
                    ],
                });
            },
            saveOwnerRepairInfo: function () {
                if (!$that.addOwnerRepairValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                if (vc.notNull($props.callBackListener)) {
                    vc.emit($props.callBackListener, $props.callBackFunction, $that.addTelHousekeepingInfo);
                    $('#addOwnerRepairModel').modal('hide');
                    return;
                }
                vc.http.apiPost(
                    '/housekeepingServPool/saveTelHousekeepingServ',
                    JSON.stringify($that.addTelHousekeepingInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        if (res.status == 200) {
                            //关闭model
                            $('#addOwnerRepairModel').modal('hide');
                            $that.clearAddOwnerRepairInfo();
                            vc.emit('housekeepingServPoolManage', 'listHousekeepingServPool', {});
                            vc.toast("登记成功");
                            return;
                        }
                        vc.toast(json);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            clearAddOwnerRepairInfo: function () {
                let _repairSettings = $that.addTelHousekeepingInfo.repairSettings;
                $that.addTelHousekeepingInfo = {
                    poolId: '',
                    servId: '',
                    houseTypeId: '',
                    shopId: vc.getCurrentCommunity().shopId,
                    servName: '',
                    applyName: '',
                    applyTel: '',
                    appointmentTime: '',
                    context: '',
                    housekeepingServs: [],
                    repairSettings: _repairSettings
                };
            },
            _initAddOwnerRepairInfo: function () {
                // $that.addTelHousekeepingInfo.appointmentTime = vc.dateTimeFormat(new Date().getTime());
                $('.addAppointmentTime').datetimepicker({
                    language: 'zh-CN',
                    fontAwesome: 'fa',
                    format: 'yyyy-mm-dd hh:ii:ss',
                    initTime: true,
                    initialDate: new Date(),
                    autoClose: 1,
                    todayBtn: true
                });
                $('.addAppointmentTime').datetimepicker()
                    .on('changeDate', function (ev) {
                        var value = $(".addAppointmentTime").val();
                        $that.addTelHousekeepingInfo.appointmentTime = value;
                    });
                //防止多次点击时间插件失去焦点
                document.getElementsByClassName('form-control addAppointmentTime')[0].addEventListener('click', myfunc)

                function myfunc(e) {
                    e.currentTarget.blur();
                }
            },
            _listRepairSettings: function (_page, _rows) {
                var param = {
                    params: {
                        page: _page,
                        row: _rows,
                        shopId: vc.getCurrentCommunity().shopId,
                        typeCd: '1001'
                    }
                };
                //发送get请求
                vc.http.apiGet('/housekeepingType.queryHousekeepingType',
                    param,
                    function (json, res) {
                        var _repairSettingManageInfo = JSON.parse(json);
                        $that.addTelHousekeepingInfo.repairSettings = _repairSettingManageInfo.data;
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _listHousekeepingServs: function (_page, _rows, hktId) {
                var param = {
                    params: {
                        page: _page,
                        row: _rows,
                        hktId: hktId,
                        shopId: vc.getCurrentCommunity().shopId
                    }
                };
                //发送get请求
                vc.http.apiGet('/housekeepingServ.queryHousekeepingServ',
                    param,
                    function (json, res) {
                        var _housekeepingServManageInfo = JSON.parse(json);
                        $that.addTelHousekeepingInfo.housekeepingServs = _housekeepingServManageInfo.data;
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            changeHouseType: function (hktId) {
                $that._listHousekeepingServs(1, 50, hktId);
            },
            changeSer: function (_resTypeId) {
                $that.addTelHousekeepingInfo.housekeepingServs.forEach(item => {
                    if (item.servId == _resTypeId) {
                        $that.addTelHousekeepingInfo.servName = item.servName;
                    }
                });
            },
        }
    });
})(window.vc);
